<?php
namespace app\api\controller\project;

// 底层控制器
use think\Controller;

class Project extends Controller {
    // 构造函数

    public function __construct() {
        // 手动把父类的方法获取过来
        parent::__construct();

        $this->ProjectModel = model( 'Project.Project' );
        $this->UserModel = model( 'User.User' );
        $this->AddressModel = model( 'User.Address' );
        $this->OrderModel = model( 'Project.Order' );
        $this->RecordModel = model( 'User.Record' );
    }

    // 添加预约

    public function add() {
        if ( $this->request->isPost() ) {
            // 获取前端传来的数据
            $userid = $this->request->param( 'userid', 0 );
            $addrid = $this->request->param( 'addrid', 0 );
            $projectid = $this->request->param( 'projectid', 0 );
            $createtime = $this->request->param( 'createtime', '' );
            $content = $this->request->param( 'content', '' );

            // 判断用户是否存在
            $userinfo = $this->UserModel->find( $userid );

            if ( !$userinfo ) {
                $this->error( '用户不存在' );
                exit;
            }

            // 用户的收货地址
            $address = $this->AddressModel->find( $addrid );

            if ( !$address ) {
                $this->error( '预约地址不存在' );
                exit;
            }

            // 判断这个收货地址是不是该用户
            if ( $address[ 'userid' ] != $userid ) {
                $this->error( '非法选择上门地址' );
                exit;
            }

            // 判断服务项是否存在
            $project = $this->ProjectModel->find( $projectid );

            if ( !$project ) {
                $this->error( '预约服务不存在，请重新选择' );
                exit;
            }

            // 将毫秒转换为秒
            $createtime = $createtime / 1000;

            // 判断是否为过期的时间
            $now = time() + 3600 * 6;
            if ( $now > $createtime ) {
                $this->error( '请选择有效的时间，在当前时间的6小时后预约' );
                exit;
            }

            // 组装数据
            $data = [
                'userid' => $userid,
                'addrid' => $addrid,
                'projectid' => $projectid,
                'content' => $content,
                'status' => 0,
                'createtime' => $createtime
            ];

            $result = $this->OrderModel->validate( 'common/Project/Order' )->save( $data );

            if ( $result === false ) {
                $this->error( $this->OrderModel->getError() );
                exit;
            } else {
                $this->success( '预约成功！请等待客服联系', '/pages/project/index' );
            }
        }
    }

    // 预约列表展示

    public function index() {
        if ( $this->request->isPost() ) {
            $userid = $this->request->param( 'userid', 0 );
            $status = $this->request->param( 'status', 0 );

            $where = [];
            // 判断用户是否存在
            $userinfo = $this->UserModel->find( $userid );

            if ( !$userinfo ) {
                $this->error( '用户不存在' );
                exit;
            }

            $where[ 'userid' ] = $userid;

            if ( $status == '0' || !empty( $status ) ) {
                $where[ 'status' ] = $status;
            }

            $result = $this->OrderModel->where( $where )->select();

            if ( $result === false ) {
                $this->error( $this->OrderModel->getError() );
                exit;
            } else {
                $this->success( '', '', $result );
                exit;
            }

        }
    }

    // 支付

    public function pay() {
        if ( $this->request->isPost() ) {
            $userid = $this->request->param( 'userid', 0 );
            $id = $this->request->param( 'id', 0 );

            // 判断用户是否存在
            $userinfo = $this->UserModel->find( $userid );

            if ( !$userinfo ) {
                $this->error( '用户不存在' );
                exit;
            }

            $orderinfo = $this->OrderModel->find( $id );

            if ( !$orderinfo ) {
                $this->error( '预约订单不存在，请重新下单' );
                exit;
            }

            // 判断订单类型是否相对应
            if ( $orderinfo[ 'status' ] != '1' ) {
                $this->error( '预约已经付款，不需要重复付款' );
                exit;
            }

            // 获取用户积分，判断积分够不够
            $point = $userinfo[ 'point' ];
            $UpdatePoint = bcsub( $point, $orderinfo[ 'price' ] );

            if ( $UpdatePoint < 0 ) {
                $this->error( '积分不足，请充值后操作' );
                exit;
            }

            // 开启事务
            // 预约订单表( update ) 用户表积分( update ) 消费记录( insert )
            $this->OrderModel->startTrans();
            $this->UserModel->startTrans();
            $this->RecordModel->startTrans();

            // 组装订单表数据
            $orderdata = [
                'id' => $id,
                'status' => 2
            ];

            // 预约订单表( update )
            $OrderStatus = $this->OrderModel->isUpdate()->save( $orderdata );

            if ( $OrderStatus === false ) {
                $this->error( $this->OrderModel->getError() );
                exit;
            }

            // 用户表积分( update )
            $UserStatus = $this->UserModel->isUpdate()->save( [ 'id' => $userid, 'point' => $UpdatePoint ] );

            if ( $UserStatus === false ) {
                // 回滚
                $this->OrderModel->rollback();
                $this->error( $this->UserModel->getError() );
                exit;
            }

            // 积分消费记录表 ( insert )
            // 组装数据
            $RecordData = [
                'point' => $orderinfo[ 'price' ],
                'content' => '预约订单的---消费的积分为：'.$orderinfo[ 'price' ],
                'state' => 6,
                'userid' => $userid
            ];

            // 插入消费记录表
            $RecordStatus = $this->RecordModel->validate( 'common/User/Record' )->save( $RecordData );

            if ( $RecordStatus === false ) {
                // 回滚
                $this->OrderModel->rollback();
                $this->UserModel->rollback();
                $this->error( $this->RecordModel->getError() );
                exit;
            }

            if ( $OrderStatus !== false && $UserStatus !== false && $RecordStatus !== false ) {
                // 提交事务
                $this->OrderModel->commit();
                $this->UserModel->commit();
                $this->RecordModel->commit();
                $this->success( '付款成功！' );
                exit;
            } else {
                // 回滚
                $this->OrderModel->rollback();
                $this->UserModel->rollback();
                $this->RecordModel->rollback();
                $this->error( '付款失败，请重新操作！' );
                exit;
            }

        }
    }

    // 撤销服务

    public function revoke() {
        if ( $this->request->isPost() ) {
            $userid = $this->request->param( 'userid', 0 );
            $id = $this->request->param( 'id', 0 );

            // 判断用户是否存在
            $userinfo = $this->UserModel->find( $userid );

            if ( !$userinfo ) {
                $this->error( '用户不存在' );
                exit;
            }

            $orderinfo = $this->OrderModel->find( $id );

            if ( !$orderinfo ) {
                $this->error( '预约订单不存在，请重新下单' );
                exit;
            }

            // 组装数据
            $data = [
                'id' => $id,
                'status' => -1,
            ];

            // 预约订单表修改状态
            $result = $this->OrderModel->isUpdate()->save( $data );

            if ( $result === false ) {
                $this->error( $this->OrderModel->getError() );
                exit;
            } else {
                $this->success( '撤销预约订单成功！请等待后台审核', '', null );
                exit;
            }
        }
    }

    // 预约详情

    public function info() {
        if ( $this->request->isPost() ) {
            $userid = $this->request->param( 'userid', 0 );
            $id = $this->request->param( 'id', 0 );

            // 判断用户是否存在
            $userinfo = $this->UserModel->find( $userid );

            if ( !$userinfo ) {
                $this->error( '用户不存在' );
                exit;
            }

            $orderinfo = $this->OrderModel->find( $id );

            if ( !$orderinfo ) {
                $this->error( '预约订单不存在，请重新下单' );
                exit;
            }

            // 获取预约订单详情
            $result = $this->OrderModel->with( 'project' )->where( [ 'order.id' => $id ] )->find();

            if ( $result === false ) {
                $this->error( $this->OrderModel->getError() );
                exit;
            } else {
                $this->success( '获取预约信息成功！', '', $result );
                exit;
            }
        }
    }

    // 预约评分

    public function rate() {
        if ( $this->request->isPost() ) {
            $userid = $this->request->param( 'userid', 0 );
            $id = $this->request->param( 'id', 0 );
            $rate = $this->request->param( 'rate', 0 );

            // 判断用户是否存在
            $userinfo = $this->UserModel->find( $userid );

            if ( !$userinfo ) {
                $this->error( '用户不存在' );
                exit;
            }

            $orderinfo = $this->OrderModel->find( $id );

            if ( !$orderinfo ) {
                $this->error( '预约订单不存在，请重新下单' );
                exit;
            }

            $data = [
                'id' => $id,
                'rate' => $rate
            ];

            $result = $this->OrderModel->isUpdate()->save( $data );

            if ( $result === false ) {
                $this->error( $this->OrderModel->getError() );
                exit;
            } else {
                $this->success( '预约订单评价成功！', '', '' );
                exit;
            }
        }
    }
}
?>